<?php

class Post extends CI_Controller {

	public function __construct() {

		parent::__construct();
		$this->output->enable_profiler(PROFILER);
		is_logged_in();
		$this->load->model("postmodel");
	}

	public function index() {
		$this -> load -> library('table');

		if ($this -> input -> post('search')) {

			if (array_key_exists('search', $_POST)) {

				$term = $this -> input -> post('search');
				//disable pagination
				$data['pagination_enabled'] = false;

			}

			if ($query = $this -> postmodel -> search($term)) {
				$data['posts'] = $query;
			}

		} else {

			
			$this -> load -> library('pagination');

			//enable pagination
			$data['pagination_enabled'] = true;

			$config['base_url'] = site_url() . '/post/index';
			$config['total_rows'] = $this -> db -> get('blog_post') -> num_rows();
			$config['per_page'] = 10;
			$config['num_links'] = 20;
			$this -> pagination -> initialize($config);
			if ($this -> uri -> segment(3)) {
				$offset = $this -> uri -> segment(3);
			}
			else{
				$offset=0;
			}

			if ($query = $this -> postmodel -> getAll($config['per_page'], $offset)) {
				$data['posts'] = $query;
			}

		}

		$data['main_content'] = "admin/post/index";
		$this -> load -> view("includes/dashboard/template", $data);
		
		
		

	}

	public function create() {
			
		//load categories for any case
		
		$this->load->model("categorymodel");
		
		$data['categories']=clean_array_for_dropdown($this -> categorymodel-> getAll(), "id", "name");
			
			
		if ($this -> form_validation -> run('post/create') == TRUE) {
	
			if($_FILES['file']){
				$image_data=$this->postmodel->do_upload();
				$_POST['file']=$image_data['file_name'];
			}

			$id = $this -> postmodel -> add($_POST);

			if ($id) {

				$this -> session -> set_flashdata('message', array('title' => 'Post Created', 'content' => 'The post has been sucessfully created', 'type' => 'alert_success'));
				
				
				
				redirect('post/index');

			} else {

				$this -> session -> set_flashdata('message', array('title' => 'Post Not Created', 'content' => 'A database Error has occur, please contact your administrator', 'type' => 'alert_error'));
				redirect('post/index');
			}

		} else {
					
		$_POST['enabled'] = (isset($_POST['enabled']) == null) ? 0 : 1;
		$_POST['featured'] = (isset($_POST['featured']) == null) ? 0 : 1;
		$_POST['comments_enabled'] = (isset($_POST['comments_enabled']) == null) ? 0 : 1;	
			
			$data['post']=$_POST;
			$data['main_content'] = "admin/post/form";						
			$this -> load -> view("includes/dashboard/template", $data);

		}
	}

	public function edit($id) {
		
		$id = (int)$id;
		$data['id'] = $id;
		$this->load->model("categorymodel");
		$data['categories']=clean_array_for_dropdown($this -> categorymodel-> getAll(), "id", "name");
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			
			if($_FILES['file']){
				$image_data=$this->postmodel->do_upload($this->input->post('file'));
			}
			
			
			if ($this -> form_validation -> run("post/edit") == TRUE) {
				if ($this -> postmodel -> updaterecord($_POST, $id)) {

					$this -> session -> set_flashdata('message', array('title' => 'Post Updated', 'content' => 'The Post has been sucessfully updated', 'type' => 'alert_success'));
					redirect('post/index');

				} else {

					$this -> session -> set_flashdata('message', array('title' => 'Post Not Updated', 'content' => 'A database Error has occur, please contact your administrator', 'type' => 'alert_error'));
					redirect('post/index');
				}

			} else {
				
			$_POST['enabled'] = (isset($_POST['enabled']) == null) ? 0 : 1;
			$_POST['featured'] = (isset($_POST['featured']) == null) ? 0 : 1;
			$_POST['comments_enabled'] = (isset($_POST['comments_enabled']) == null) ? 0 : 1;	
			
				$data['post']=$_POST;

				$data['main_content'] = "admin/post/form";
				$this -> load -> view("includes/dashboard/template", $data);
			}

		} else {


			$data['post'] = $this -> postmodel -> getById($id);
			if ($data['post']) {
				$data['main_content'] = "admin/post/form";
				$this -> load -> view("includes/dashboard/template", $data);
			} else {
				$this -> session -> set_flashdata('message', array('title' => 'Not able to retrieve post registry', 'content' => 'A database Error has occur, please contact your administrator', 'type' => 'alert_warning'));
				redirect('post/index');
			}
		}
	}

	public function delete($id) {
			
		$deleted_post=$this -> postmodel -> delete($id);
		if($this->postmodel->delete_file($deleted_record['file']))
		{
		$this -> session -> set_flashdata('message', array('title' => 'Post deleted', 'content' => 'The Post has been sucessfully deleted', 'type' => 'alert_success'));
		redirect('post/index');
		}
		else{
			echo "wasnt able to delete the post"; die();
		}
	}

}
?>